
//JETNUKE

/*
 * Copyright (c) 2004-2006 by Andrea Mazzolini [andreamazzolini@users.sourceforge.net]
 * http://www.jetnuke.org
 * 
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 *  
 */

package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.BlogPost;

public class GenericBlogPostDAO extends AutoKeys implements
		org.jetnuke.dao.BlogPostDAO {

	public BlogPost retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogPostModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		BlogPost s = new BlogPost();
		
		if (rs.next()) {
			fillBlogPostFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	protected void fillBlogPostFromResultSet(BlogPost s, ResultSet rs) throws Exception{
		s.setAID(rs.getInt("POST_AUTHOR"));
		s.setCOMMENTSTATUS(rs.getString("COMMENT_STATUS"));
		s.setGUID(rs.getString("GUID"));
		s.setID(rs.getInt("ID"));
		s.setPINGED(rs.getString("PINGED"));
		s.setPINGSTATUS(rs.getString("PING_STATUS"));
		s.setPOSTCATEGORY(rs.getInt("POST_CATEGORY"));
		s.setPOSTCONTENT(rs.getString("POST_CONTENT"));
		s.setPOSTDATE(rs.getTimestamp("POST_DATE"));
		s.setPOSTEXCERPT(rs.getString("POST_EXCERPT"));
		s.setPOSTMODIFIED(rs.getTimestamp("POST_MODIFIED"));
		s.setPOSTPASSWORD(rs.getString("POST_PASSWORD"));
		s.setPOSTSTATUS(rs.getString("POST_STATUS"));
		s.setPOSTTITLE(rs.getString("POST_TITLE"));
		s.setTOPING(rs.getString("TO_PING"));
		
	}

	public void update(BlogPost bp) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogPostModel.update"));
		long time=new GregorianCalendar().getTime().getTime();
		p.setInt(1,bp.getAID());
		p.setTimestamp(2,new Timestamp(bp.getPOSTDATE().getTime()));
		p.setString(3,bp.getPOSTCONTENT());
		p.setString(4,bp.getPOSTTITLE());
		p.setInt(5,bp.getPOSTCATEGORY());
		p.setString(6,bp.getPOSTEXCERPT());
		p.setString(7,bp.getPOSTSTATUS());
		p.setString(8,bp.getCOMMENTSTATUS());
		p.setString(9,bp.getPINGSTATUS());
		p.setString(10,bp.getPOSTPASSWORD());
		p.setString(11,bp.getTOPING());
		p.setString(12,bp.getPINGED());
		p.setTimestamp(13,new Timestamp(time));
		p.setString(14,bp.getGUID());
		p.setInt(15,bp.getID());
		p.executeUpdate();
		p.close();
		
	}

	public int addNew(BlogPost bp) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("BlogPostModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("BlogPostModel.lastGeneratedBlogPostID"));
		initBlogPost(bp, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
	}

	protected void initBlogPost(BlogPost bp, PreparedStatement p) throws Exception{
		long time=new GregorianCalendar().getTime().getTime();
		p.setInt(1,bp.getAID());
		p.setTimestamp(2,new Timestamp(time));
		p.setString(3,bp.getPOSTCONTENT());
		p.setString(4,bp.getPOSTTITLE());
		p.setInt(5,bp.getPOSTCATEGORY());
		p.setString(6,bp.getPOSTEXCERPT());
		p.setString(7,bp.getPOSTSTATUS());
		p.setString(8,bp.getCOMMENTSTATUS());
		p.setString(9,bp.getPINGSTATUS());
		p.setString(10,bp.getPOSTPASSWORD());
		p.setString(11,bp.getTOPING());
		p.setString(12,bp.getPINGED());
		p.setTimestamp(13,new Timestamp(time));
		p.setString(14,bp.getGUID());
	}

	public List retrieveByUser(int aid) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogPostModel.selectByUser"));
		p.setInt(1,aid);	
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	
	public List retrieveByUserOrdered(int aid) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogPostModel.selectByUserOrdered"));
		p.setInt(1,aid);	
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	
	

	public List retrieveByUserCat(int aid, int cat_id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogPostModel.selectByUserCat"));
		p.setInt(1,aid);	
		p.setInt(2,cat_id);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	protected List processSelectAll(ResultSet rs) throws Exception{
		List list = new ArrayList();
		while (rs.next()) {
			BlogPost u = new BlogPost();
			fillBlogPostFromResultSet(u,rs);			
			list.add(u);
		}
		return list;
	}

	public List retrieveLast(int limit) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogPostModel.selectLast"));
		p.setInt(1,limit);	
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	public List retrievePublishByUser(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogPostModel.selectPublishByUser"));
		p.setInt(1,id);	
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	public List retrievePublishByUserCat(int id, int cat) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogPostModel.selectPublishByUserCat"));
		p.setInt(1,id);	
		p.setInt(2,cat);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	public List retrievePublishByUser(int id, int maxnumberpost) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogPostModel.selectPublishByUserLimit"));
		p.setInt(1,id);
		p.setInt(2,maxnumberpost);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	public List retrieveDraftByUser(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogPostModel.selectDraftByUser"));
		p.setInt(1,id);	
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	public List retrievePrivateByUser(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogPostModel.selectPrivateByUser"));
		p.setInt(1,id);	
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	public void changeCategory(int oldCat, int newCat) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogPostModel.changeCategory"));
		p.setInt(1,newCat);
		p.setInt(2,oldCat);
		p.executeUpdate();
		p.close();

		
	}

	public Set getPostDays(int aid, int month,int year) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogPostModel.selectPostDays"));
		p.setInt(1,aid);
		p.setInt(2,month);
		p.setInt(3,year);
		ResultSet rs = p.executeQuery();
		Set set=new HashSet();
		while(rs.next()){
			set.add(rs.getString(1));
		}
		rs.close();
		p.close();
		
		return set;
	}

	public List retrievePublishByUser(int id, Calendar cal, int maxnumberpost) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BlogPostModel.selectPublishByUserDateLimit"));
		p.setInt(1,id);
		p.setTimestamp(2,new Timestamp(cal.getTime().getTime()));
		p.setInt(3,maxnumberpost);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

}

